package com.colorfruit.framework.common.util;

import com.colorfruit.framework.common.constant.FrameWorkConstant;
import com.colorfruit.framework.model.admin.CommonDate;

import java.math.BigDecimal;
import java.util.Date;

public class BusinessUtil {
    /**
     * 计算利润
     * 每件的利润按采购价+物流成本和的百分之五，限不低于5元，不高于25元，  2019.4.19日
     * @param profitBefore
     * @return
     */
    public static BigDecimal calculateMoney(BigDecimal profitBefore) {
        //计算后的值
        BigDecimal profitAfter =null;
        //将值转换成double
        double profit = profitBefore.doubleValue();
        //大规则转换
        if (profit <= 5){
            profitAfter = new BigDecimal(5);
        }else if (profit >= 25){
            profitAfter = new BigDecimal(25);
        }else {
            profitAfter = new BigDecimal(profit);
        }
        return profitAfter;
    }

    /**
     * 尾数向上取0.5元或1.0元
     * @param money
     * @return
     */
    public static BigDecimal setScale(BigDecimal money){
        BigDecimal afterScale = null;
        double price = money.doubleValue();
        //小规则转换
        long profitIntegralPart = Math.round(price);
        long profitFractionPart = 0;
        String priceString = "" + price;
        System.out.println(priceString);
        String[] priceArray = priceString.split("\\.");
        if (priceArray[1].length() > 0){
            String fractionPart = priceArray[1].substring(0, 1);
            int fractionInteger = Integer.parseInt(fractionPart);
            if (fractionInteger == 0){
                profitFractionPart = 0;
            }else if (fractionInteger <= 5){
                profitFractionPart = 5;
            }else if (profitFractionPart <= 9){
                profitFractionPart = 10;
            }
        }
        afterScale = new BigDecimal(profitIntegralPart).add(new BigDecimal(profitFractionPart).divide(new BigDecimal(10)));
        return afterScale;
    }

    /**
     * 获取日期
     * @return
     */
    public  static CommonDate getCommonDate() {
        Date theDay = new Date();
        Date theDayBegin = DataConvertUtil.getTheDayBegin(theDay);
        Date theDayEnd = DataConvertUtil.getTheDayEnd(theDay);
        Date sevenDayBefore = DataConvertUtil.getDayBeforStartTime(FrameWorkConstant.SEVEN_DAY);
        return new CommonDate(theDayBegin, theDayEnd, sevenDayBefore);
    }

    /**
     * 干掉最后的逗号
     * @param sourceString
     * @return
     */
    public static String killEndComma(String sourceString){
        if (!DataValidateUtil.verifyString(sourceString)){
            return sourceString;
        }
        if (sourceString.endsWith(",")){
            return sourceString.substring(0, sourceString.length()-1);
        }
        return sourceString;
    }
    public static void main(String[] args) {

        String s = killEndComma("");
        System.out.println(s);
    }
}
